package com.xzit.rental.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzit.rental.entity.Permission;
import com.xzit.rental.entity.User;
import com.xzit.rental.mapper.PermissionMapper;
import com.xzit.rental.mapper.UserMapper;
import com.xzit.rental.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzit.rental.utils.RouteTreeUtils;
import com.xzit.rental.vo.RolePermissionVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 何时说晚安
 * @since 2024-12-05
 */
@Service
@Transactional
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Resource
    private UserMapper userMapper;

    @Override
    public List<Permission> selectPermissionListByUserId(Integer userId) {
        return baseMapper.selectPermissionListByUserId(userId);
    }

    @Override
    public List<Permission> selectList() {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("order_num");
        List<Permission> permissionList = baseMapper.selectList(queryWrapper);
        return RouteTreeUtils.buildMenuTree(permissionList,0);
    }

    @Override
    public List<Permission> selectTree() {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("order_num");
        queryWrapper.ne("permission_type",2);
        List<Permission> permissionList = baseMapper.selectList(queryWrapper);
        Permission permission = new Permission();
        permission.setId(0).setPid(-1).setPermissionLabel("根目录");
        permissionList.add(permission);
        return RouteTreeUtils.buildMenuTree(permissionList,-1);
    }

    @Override
    public boolean hasChildren(Integer id) {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",id);
        return baseMapper.selectCount(queryWrapper)>0;
    }

    /**
     * 查询创建权限树形结构
     * @param userId 用户id
     * @param roleId 角色id
     * @return
     */
    @Override
    public RolePermissionVo selectPermissionTree(Integer userId, Integer roleId) {
        User user = userMapper.selectById(userId);
        // 当前用户的权限信息
        List<Permission> list=null;
        // 如果用户是管理员，查询出所有权限信息，否则查询出当前用户的权限信息
        if(user!=null&&user.getIsAdmin()==1){
            list=baseMapper.selectList(null);
        }else {
            list=baseMapper.selectPermissionListByUserId(userId);
        }
        // 将登陆用户所拥有的权限信息封装到树形结构中
        List<Permission> permissions = RouteTreeUtils.buildMenuTree(list, 0);
        // 查询出要分配的角色原有的权限
        List<Permission> rolePermissions = baseMapper.selectPermissionListByRoleId(roleId);
        // 找出包含部分，放在Object[]数组中
        List<Integer> ids=new ArrayList<>(list.stream().map(Permission::getId).toList());
        List<Integer> ids1=new ArrayList<>(rolePermissions.stream().map(Permission::getId).toList());
        ids.retainAll(ids1);
        Object[] array = ids.toArray();

        // 构建角色权限视图对象并返回
        RolePermissionVo rolePermissionVo = new RolePermissionVo();
        rolePermissionVo.setCheckedList(array).setPermissionList(permissions);
        return rolePermissionVo;

    }
}
